package com.fingard.dsp.bank.directbank.gzelink01;

import com.fingard.FGBiz;
import com.fingard.dsp.bank.directbank.bankInterface.IAcceptNotice;
import com.fingard.generalobj.NoticeReq;
import com.fingard.generalobj.NoticeResp;
import com.fingard.net.listener.SocketReceiver;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;

public class GZELINK01AcceptNotice extends GZELINK01Base implements
        IAcceptNotice {

    @Override
    public NoticeResp dealNotice(NoticeReq pNtReq) throws Exception {
        NoticeResp resp = new NoticeResp();
        String tmpFileName = FGBiz.DSPHOME_Parent + File.separator + "signnotify";
        try {
			/*p_req="<?xml version=\"1.0\" encoding=\"GBK\"?><GZELINK>"
					+ "<INFO><TRX_CODE>311007</TRX_CODE><VERSION>03</VERSION>"
					+ "<DATA_TYPE>2</DATA_TYPE><REQ_SN>2019041111385654238</REQ_SN>"
					+ "<RET_CODE>0000</RET_CODE><ERR_MSG>处理完成</ERR_MSG>"
					+ "<SIGNED_MSG>4fcb3ed5b2c6983de14fc9e2cd43f151360a55c7110b93d87799deddfed58c58d6760d865dae72d17cd"
					+ "293b91a9e52e7433a1ff4c24f2d8add6b294e42687ef3e1ba5cf68afed868856ea7e833dc14bd7932f8434d1e9121ef"
					+ "8337f2d62da690c786c7502b11f9b0df866edecb0b8989e51f7cddf9e3bcf1c615cd2f6de6831</SIGNED_MSG></INFO>"
					+ "<BODY><RET_DETAILS><RET_DETAIL><NOTIFY_TYPE>0</NOTIFY_TYPE><TRANS_STATUS>0000</TRANS_STATUS>"
					+ "<TRANS_INFO>交易成功</TRANS_INFO><PROTOCOL_REQ_SN>155495281402979917925</PROTOCOL_REQ_SN>"
					+ "<PROTOCOL_NO>1234</PROTOCOL_NO><SIGN_TIME>20190411113810</SIGN_TIME><UNSIGN_TIME></UNSIGN_TIME>"
					+ "<SIGN_STATUS>0</SIGN_STATUS><MERCHANT_ID>000191400100690</MERCHANT_ID><BANK_CODE>01020000</BANK_CODE>"
					+ "<ACCOUNT_NO>9558803602132597999</ACCOUNT_NO><ACCOUNT_NAME>陈霄峰111</ACCOUNT_NAME><ACCOUNT_TYPE>00</ACCOUNT_TYPE>"
					+ "<ID_TYPE>0</ID_TYPE><ID>440822197606250035</ID><TEL>15099973611</TEL><BUSINESS_CODE>10405</BUSINESS_CODE>"
					+ "<ACCOUNT_PROP>0</ACCOUNT_PROP><LGL_REP_NM></LGL_REP_NM><LGL_REP_ID_TP></LGL_REP_ID_TP>"
					+ "<LGL_REP_ID_NO></LGL_REP_ID_NO><DISABLE_DATE>20290411</DISABLE_DATE><SINGLE_LIMIT>5000000</SINGLE_LIMIT>"
					+ "<LIMIT_PERIOD_UNIT>02</LIMIT_PERIOD_UNIT><MAX_CNT_LIMIT>500</MAX_CNT_LIMIT></RET_DETAIL></RET_DETAILS>"
					+ "</BODY></GZELINK>";*/
            String p_req = pNtReq.getReqString();
            WriteBankLogLn("接收到广银联通知报文：" + pNtReq.getFullString());
            if (p_req.length() == 0) {
                String tmpStrRet = "空报文";
                WriteBankLogLn("响应：" + tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
                return resp;
            }

            //写入文件的字符串
            StringBuilder reader_str = new StringBuilder();
            StringBuilder tmpSbMsg = new StringBuilder();
            XmlTextReader tmpBkReader = loadBankRespXmlReader(p_req, tmpSbMsg);
            if (tmpBkReader != null) {
                String tmpRespCode = tmpBkReader.getFirstText("/GZELINK/INFO/RET_CODE");
                //只有0000才有报文体
                if ("0000".equals(tmpRespCode)) {
                    if ("0000".equals(tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/TRANS_STATUS"))) {
                        //签约、解约成功
                        String tmpRespInfo = tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/TRANS_INFO");
                        String respCode = StringHelper.joinNotEmpty("+", tmpRespCode, tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/RET_CODE"));
                        reader_str.append("<RET_DETAIL>");
                        reader_str.append("<BankCode>GZELINK01</BankCode>");
                        reader_str.append("<RespCode>" + respCode + "</RespCode>");
                        reader_str.append("<RespInfo>" + tmpRespInfo + "</RespInfo>");
                        reader_str.append("<SignState>success</SignState>");
                        String pay_act = tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/ACCOUNT_NO");
                        reader_str.append("<PayAct>" + pay_act + "</PayAct>");
                        reader_str.append("<ReqSeqID>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/PROTOCOL_REQ_SN") + "</ReqSeqID>");
                        reader_str.append("<AuthCode>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/PROTOCOL_NO") + "</AuthCode>");
                        reader_str.append("<NOTIFY_TYPE>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/NOTIFY_TYPE") + "</NOTIFY_TYPE>");
                        reader_str.append("<PayName>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/ACCOUNT_NAME") + "</PayName>");
                        reader_str.append("<CardType>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/ACCOUNT_TYPE") + "</CardType>");
                        reader_str.append("<CertType>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/ID_TYPE") + "</CertType>");
                        reader_str.append("<CertNum>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/ID") + "</CertNum>");
                        reader_str.append("<CellPhone>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/TEL") + "</CellPhone>");
                        reader_str.append("<LegalName>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/LGL_REP_NM") + "</LegalName>");
                        reader_str.append("<LegalCertType>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/LGL_REP_ID_TP") + "</LegalCertType>");
                        reader_str.append("<LegalCertNum>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/LGL_REP_ID_NO") + "</LegalCertNum>");

                        reader_str.append("<DeadLine>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/DISABLE_DATE") + "</DeadLine>");
                        reader_str.append("<SingleLimit>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/SINGLE_LIMIT") + "</SingleLimit>");
                        reader_str.append("<PeriodLimit>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/LIMIT_PERIOD_UNIT") + "</PeriodLimit>");
                        reader_str.append("<CountLimit>" + tmpBkReader.getFirstText("/GZELINK/BODY/RET_DETAILS/RET_DETAIL/MAX_CNT_LIMIT") + "</CountLimit>");
                        reader_str.append("</RET_DETAIL>");
                        File dir = new File(tmpFileName);
                        if (!dir.exists()) {
                            dir.mkdir();
                        }
                        File tmpFile = new File(tmpFileName + File.separator + pay_act + ".txt");
                        if (!tmpFile.exists()) {
                            tmpFile.createNewFile();
                            FileOutputStream fos = null;
                            PrintStream ps = null;
                            try {
                                fos = new FileOutputStream(tmpFile);
                                ps = new PrintStream(fos);
                                ps.println(reader_str);
                            } catch (Exception e) {
                                e.printStackTrace();
                            } finally {
                                if (fos != null) {
                                    fos.close();
                                }
                                if (ps != null) {
                                    ps.close();
                                }
                            }
                        }
                    }
                }
                String tmpStrRet = "0000";
                WriteBankLogLn("响应：" + tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
                return resp;
            } else {
                String tmpStrRet = "报文格式错误";
                WriteBankLogLn("响应：" + tmpStrRet);
                resp.respBytes = tmpStrRet.getBytes(pNtReq.reqCharset);
                return resp;
            }
        } catch (Exception e) {
            WriteBankLogLn(e);
        }
        return resp;
    }

    @Override
    public void getContentLength(SocketReceiver p_receiver) throws Exception {
        // TODO Auto-generated method stub

    }

}
